package cn.dy.workatten.dao;

import cn.dy.base.framework.db.DBConnectPool;
import cn.dy.base.framework.db.DBUtil;
import cn.dy.base.framework.db.util.DBExecuteUtil;
import cn.dy.workatten.pojo.ContactGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class ContactGroupDao {
	private DBExecuteUtil db;
	private DataSource dataSource;

	public ContactGroupDao() {
		this.db = new DBExecuteUtil();
		this.dataSource = DBConnectPool.create().getDataSource();
	}

	public ContactGroup getContactGroup(int targer_id) {
		Map params = new HashMap();
		ContactGroup contactGroup = null;
		String sql = "select e.* from ecc_contact_group e where e.id=:targer_id";
		params.put("targer_id", Integer.valueOf(targer_id));
		return contactGroup = (ContactGroup) this.db.queryObject(sql, params, ContactGroup.class);
	}

	public int createContactGroup(ContactGroup contactGroup) {
		contactGroup.setIs_fixed("N");
		contactGroup.setSts("1");
		contactGroup.setOrder_index(contactGroup.getId());
		contactGroup.setIs_syn("0");
		return this.db.insert(contactGroup, "ecc_contact_group");
	}

	public int updateContactGroup(ContactGroup contactGroup) {
		contactGroup.setIs_fixed("Y");
		return this.db.update(contactGroup, "ecc_contact_group", Integer.valueOf(contactGroup.getId()));
	}

	public int updateCantactGroup(int old_id, ContactGroup contactGroup) {
		Map params = new HashMap();
		String sql = "update ecc_contact_group set name=:name,strata=:strata,introduction=:introduction,address=:address,postcode=:postcode,phone=:phone,fax=:fax,contact=:contact  where id=:id";
		params.put("id", Integer.valueOf(old_id));
		params.put("name", contactGroup.getName());
		params.put("strata", contactGroup.getStrata());
		params.put("introduction", contactGroup.getIntroduction());
		params.put("address", contactGroup.getAddress());
		params.put("postcode", contactGroup.getPostcode());
		params.put("phone", contactGroup.getPhone());
		params.put("fax", contactGroup.getFax());
		params.put("contact", contactGroup.getContact());
		return this.db.executeSql(sql, params);
	}

	public int updateCantactGroupId(int old_id, int new_id) {
		Map params = new HashMap();
		String sql = "update ecc_contact_group e set e.parent_id=:new_id where parent_id=:old_id";
		params.put("old_id", Integer.valueOf(old_id));
		params.put("new_id", Integer.valueOf(new_id));
		return this.db.executeSql(sql, params);
	}

	public int updateCantactGroupById(int src_id, int targer_id, int order_index) {
		Map params = new HashMap();
		String sql = "update ecc_contact_group e set e.parent_id=:target_id,e.order_index=:order_index where e.id=:src_id";
		params.put("targer_id", Integer.valueOf(targer_id));
		params.put("src_id", Integer.valueOf(src_id));
		params.put("order_index", Integer.valueOf(order_index));
		return this.db.executeSql(sql, params);
	}

	public int updateCantactGrouptOderIndex(int id, int order_index) {
		Map params = new HashMap();
		String sql = "update ecc_contact_group set order_index=:order_index where id=:id";
		params.put("id", Integer.valueOf(id));
		params.put("order_index", Integer.valueOf(order_index));
		return this.db.executeSql(sql, params);
	}

	public int delGroup(int id) {
		Map params = new HashMap();
		String sql = "delete from ecc_contact_group where id=:id";
		params.put("id", Integer.valueOf(id));
		return this.db.executeSql(sql, params);
	}

	public int getCountById(int id) throws Exception {
		Map params = new HashMap();
		String sql = "select count(1) from ecc_contact_group e where e.parent_id=:id and sts='1'";
		params.put("id", Integer.valueOf(id));
		return ((Integer) this.db.queryObject(sql, params, Integer.class)).intValue();
	}

	public int getCount(int id) {
		Map params = new HashMap();
		String sql = "select count(1) from ecc_contact_group e where e.id=:id";
		params.put("id", Integer.valueOf(id));
		return ((Integer) this.db.queryObject(sql, params, Integer.class)).intValue();
	}

	public List getContactGroupByParentId(int parentId) {
		List list = null;
		Map params = new HashMap();
		String sql = "select * from ecc_contact_group e where e.parent_id=:parent_id order by order_index asc";
		params.put("parentId", Integer.valueOf(parentId));
		return list = this.db.queryList(sql, params, ContactGroup.class);
	}

	public int updataGontactGroup(int id, int order_index) {
		Map params = new HashMap();
		String sql = "update ecc_contact_group set order_index=:order_index where id=:id";
		params.put("id", Integer.valueOf(id));
		params.put("order_index", Integer.valueOf(order_index));
		return this.db.executeSql(sql, params);
	}

	public int getUniqueId() {
		return DBUtil.getSequence(new JdbcTemplate(this.dataSource), "seq_ecc_contact_group");
	}

	public int chekcName(String name) {
		Map params = new HashMap();
		String sql = "select count(*) from ecc_contact_group where name=:name";
		params.put("name", name);
		return ((Integer) this.db.queryObject(sql, params, Integer.class)).intValue();
	}

	public String getName(int id) {
		Map params = new HashMap();
		String sql = "select name from ecc_contact_group where id=:id";
		params.put("id", Integer.valueOf(id));
		return ((String) this.db.queryObject(sql, params, String.class)).toString();
	}

	public int getId(String name, int parent_id) {
		Map params = new HashMap();
		ContactGroup contactGroup = null;
		String sql = "select * from ecc_contact_group where name=:name and parent_id=:parent_id";
		params.put("name", name);
		params.put("parent_id", Integer.valueOf(parent_id));
		contactGroup = (ContactGroup) this.db.queryObject(sql, params, ContactGroup.class);
		int id = contactGroup.getId();
		return id;
	}

	public List getAllGroup(int id) {
		Map params = new HashMap();
		List<ContactGroup> groupList = new ArrayList();
		String sql = "SELECT * FROM ecc_contact_group ecg WHERE ecg.PARENT_ID=:id";
		params.put("id", Integer.valueOf(id));
		groupList = this.db.queryList(sql, params, ContactGroup.class);
		return groupList;
	}

	public int getCorpId(int group_id) {
		Map params = new HashMap();
		String sql = "SELECT DISTINCT ecg.CORP_ID FROM  ecc_contact_group ecg WHERE ecg.ID=:group_id";
		params.put("group_id", Integer.valueOf(group_id));
		return ((Integer) this.db.queryObject(sql, params, Integer.class)).intValue();
	}

	public String getCorpName() {
		Map params = new HashMap();
		String sql = "SELECT sci.NAME FROM sm_corporation_info sci WHERE id=1";
		return (String) this.db.queryObject(sql, String.class);
	}
}
